package com.gm.manager.common.tools;

import com.gm.manager.common.exception.ParameterException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author zhuZhaoYang
 * @date 2021/7/12 9:57
 */
public class ValidatorUtils {
    private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @param groups 待校验的组
     */
    @SafeVarargs
    public static <T> void validateEntity(Object object, Class<T>... groups) {
        System.out.println("参数校验  " + object.toString());
        Set<ConstraintViolation<Object>> constraintViolations = VALIDATOR.validate(object);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation<Object> next = constraintViolations.iterator().next();
            throw new ParameterException(next.getMessage());
        }
    }

}
